package com.doyens.item.controller;

import com.doyens.item.pojo.Category;
import com.doyens.item.service.CategoryService;
import com.netflix.ribbon.proxy.annotation.Http;
import com.sun.net.httpserver.HttpsConfigurator;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public ResponseEntity<List<Category>> queryCategoryByPid(@RequestParam(value = "pid",defaultValue = "0") Long pid){
        if (pid == null || pid <0 ){
           //参数不合法..400
          // return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
            return ResponseEntity.badRequest().build();
        }
        //参数合法
        List<Category>  categories = categoryService.queryCategoryByPid(pid);
        //判断是否有数据,没有数据返回404
        if (CollectionUtils.isEmpty(categories)){
           // return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            return ResponseEntity.notFound().build();//优化
        }
        //查询成功返回200
        return ResponseEntity.ok(categories);
    }

    //根据分类的id查询分类名称   1,2,3
    @GetMapping
    public ResponseEntity<List<String>> queryNameByIds(@RequestParam("ids") List<Long> ids){
        List<String> names = this.categoryService.queryNamesByIds(ids);
        if (CollectionUtils.isEmpty(names)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(names);
    }


}
